package com.baosight.sczx.zm.cs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCSNumber;
import com.baosight.sczx.zm.cs.domain.ZMCSSystemSettingCS;

/**
 * 参数设置-系统基础设置
 * <p>Title: ServiceZMCS11.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-16 上午03:14:09  严洪浩  Create
 *</pre>
 */
public class ServiceZMCS11 extends ServiceEPBase {
	
	/**
	 * 初始化系统基础管理参数设置
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15下午02:26:48 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({"rawtypes", "unchecked" })
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		List _check = dao.query("ZMCSSystemSettingCS.query", new HashMap<String, String>());
		inInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCSSystemSettingCS().eiMetadata);
		
		if(_check == null || _check.size() < 1) {
			List _resultList = new ArrayList();
			Map<String, Object> _tempMap = new HashMap<String, Object>();
			_tempMap.put("fdKeyId", TablePk.getInstance().getNextValue("T_CI_SYSTEMPARAMETERSSETTING"));
			_tempMap.put("fdSystemModel", "0");//系统模式；0矿级模式 1分子公司 2集团公司
			_tempMap.put("fdMiningType", "0");//矿区形式; 0单矿区 1多矿区
			_tempMap.put("fdSystemName", "安全生产管控信息系统");
			_tempMap.put("fdVersion", "3.0.0");
			_tempMap.put("fdMaxUser", "10");
			_tempMap.put("fdEngineeringNo", "0");//工程编号; 0自动,1手动,2生成
			_tempMap.put("fdEngineeringNoRule", "[GC]+GZM+WM{4}");//工程编号规则
			_tempMap.put("fdWorkingfaceNo", "0");//工作面编号; 0自动,1手动,2生成
			_tempMap.put("fdWorkingfaceNoRule", "[G]+KJ+WM{3}");//工作面编号规则
			_tempMap.put("fdRoadwayNo", "0");//巷道编号; 0自动,1手动,2生成
			_tempMap.put("fdRoadwayNoRule", "[X]+KJ+WM{3}");//巷道编号规则
			_tempMap.put("fdOtherDesignNo", "0");//其他设计编号; 0自动,1手动,2生成
			_tempMap.put("fdOtherDesignNoRule", "[QT]+KJ+WM{3}");//其他设计规则
			dao.insert("ZMCSSystemSettingCS.insert", _tempMap); //如果初始数据不存在，添加初始数据
			_resultList.add(_tempMap);
			inInfo.getBlock(EiConstant.resultBlock).addRows(_resultList);
		}
		else {
			inInfo.getBlock(EiConstant.resultBlock).addRows(_check);
		}
		
		//后台不再构造下拉列表的Block
		//inInfo.addBlock(getSystemModel());
		//inInfo.addBlock(getMiningType());
		
		return inInfo;
	}
	
	/**
	 * 第一个EFRegion修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15下午03:58:11 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCSSystemSettingCS.update");
		changeInitialData(outInfo);//如果修改成功则重新刷新数据
		return outInfo;
	}
	
	/**
	 * 第二个EFRegion修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15下午03:58:11 严洪浩
	 * </p>
	 *
	 */
	public EiInfo updateRuleSet(EiInfo inInfo) {
		String fdWorkingfaceNoRule = inInfo.getBlock("result").getCellStr(0, "fdWorkingfaceNoRule");//工作面编号规则
		boolean result = new SystemSettingCSNumber("1").checkValidator(fdWorkingfaceNoRule);
		if(!result) {
			inInfo.setStatus(-1);
			inInfo.setMsg("工作面编号规则错误！");
			return inInfo;
		}
		
		String fdRoadwayNoRule = inInfo.getBlock("result").getCellStr(0, "fdRoadwayNoRule");//巷道编号规则
		result = new SystemSettingCSNumber("2").checkValidator(fdRoadwayNoRule);
		if(!result) {
			inInfo.setStatus(-1);
			inInfo.setMsg("巷道编号规则错误！");
			return inInfo;
		}
		
		String fdOtherDesignNoRule = inInfo.getBlock("result").getCellStr(0, "fdOtherDesignNoRule");//其他设计规则
		result = new SystemSettingCSNumber("3").checkValidator(fdOtherDesignNoRule);
		if(!result) {
			inInfo.setStatus(-1);
			inInfo.setMsg("其他编号规则错误！");
			return inInfo;
		}
		
		String fdEngineeringNoRule = inInfo.getBlock("result").getCellStr(0, "fdEngineeringNoRule");//工程编号规则
		result = new SystemSettingCSNumber("4").checkValidator(fdEngineeringNoRule);
		if(!result) {
			inInfo.setStatus(-1);
			inInfo.setMsg("工程编号规则错误！");
			return inInfo;
		}
		
		EiInfo outInfo = super.update(inInfo, "ZMCSSystemSettingCS.updateRuleSet");
		changeInitialData(outInfo);//如果修改成功则重新刷新数据
		return outInfo;
	}
	
	/**
	 * 如果修改成功则刷新数据
	 * <p>Function: changeInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午02:59:02 严洪浩
	 * </p>
	 *
	 */
	private void changeInitialData(EiInfo outInfo){
		if(outInfo != null && outInfo.getStatus() != -1) {
			CmesConstants.SYSTEMSETTING_CS = 1;
		}
	}
}
